package com.nd.android.u.filestoragesystem.business;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.json.JSONException;

import android.content.Context;

import com.common.android.utils.http.HttpException;
import com.nd.android.u.filestoragesystem.business.bean.UploadImageParams;
import com.nd.android.u.filestoragesystem.business.com.NdFileStorageSdk;
import com.nd.android.u.filestoragesystem.externalInterface.FileConstant;
import com.nd.android.u.filestoragesystem.externalInterface.FileConstant.FileClass;
import com.nd.android.u.filestoragesystem.externalInterface.FileConstant.FilePath;
import com.nd.android.u.filestoragesystem.externalInterface.FileConstant.MxResouceId;
import com.nd.android.u.filestoragesystem.externalInterface.FileConstant.RequestType;
import com.nd.android.u.filestoragesystem.externalInterface.FileConstant.Sort;
import com.nd.android.u.filestoragesystem.externalInterface.FileUtilFactory;
import com.nd.android.u.filestoragesystem.externalInterface.IExternalInterface;
import com.nd.android.u.filestoragesystem.externalInterface.IFile;
import com.nd.android.u.filestoragesystem.externalInterface.IFolder;
import com.product.android.business.ApplicationVariable;
/**
 * 文件系统外部调用具体实现
 * 
 * <br>Created 2014-6-27 下午2:50:22
 * @version  
 * @author   huangszh		
 *
 * @see
 */
public class ExternalInterfaceImp implements IExternalInterface{
    
    
    @Override
    public ArrayList<IFolder> getMxAlbums(Context context,int type, String sid, long id, int requestClass,
            long id_max, long id_min,String sort,int page_size) throws  HttpException {
        
        try {
            return new NdFileStorageSdk(context).getMxAlbums(type, sid, id, requestClass, 
                    id_max, id_min,sort,page_size);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public ArrayList<IFile> getFileList(Context context,int type, String sid, long id, long folderid,
            String path,long id_max,long id_min, int page_size) throws  HttpException {
        
        try {
            return new NdFileStorageSdk(context).getFileList(type, sid, id, folderid,path,id_max,id_min,page_size);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public ArrayList<IFolder> getAllMxAlbums(Context context, int type, String sid, long id,
            int requestClass) throws  HttpException {
        
        ArrayList<IFolder> allFolders = new ArrayList<IFolder>();
        long max_id = FileConstant.ID_MAX;
        ArrayList<IFolder> folders;
        int page_size = FileConstant.FILE_SYSTEM_MAX_SIZE;
        boolean needGetMore = false;
        do {
            folders = FileUtilFactory.getInstance().getExternalInterface().
                    getMxAlbums(ApplicationVariable.INSTANCE.applicationContext, 
                            RequestType.RESOURCE, ApplicationVariable.INSTANCE.getLocalSid(), 
                            MxResouceId.PICTURE_ALBUM_ID, FileClass.FOLDER_LIST, 
                            max_id,0, Sort.TIME,page_size);
            
            if(folders == null ) {
                needGetMore = false;
            } else {
                if(folders.size() == page_size) {
                    max_id = folders.get(folders.size() - 1).getCreateTime();
                    needGetMore = true;
                } else {
                    needGetMore = false;
                }
                allFolders.addAll(folders);
            }
            
        } while (needGetMore);
        
        return allFolders;
    }

    @Override
    public boolean deletePhotos(Context context,int type,String sid,long uid,ArrayList<Long> fids) throws  HttpException {
        
        try {
            return new NdFileStorageSdk(context).deletePhotos(type,sid,uid,fids);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public ArrayList<IFile> getMovieFileList(Context context, int type, String sid, long id,
            long id_max, long id_min, int page_size) throws HttpException {
        
        return getFileList(context, type, sid, id, 0,FilePath.MOIVE, id_max, id_min, page_size);
    }

    @Override
    public ArrayList<IFile> getPersonalPhotoList(Context context,int type, String sid, long id, 
            long id_max,long id_min, int page_size) throws HttpException {
        
        try {
            return new NdFileStorageSdk(context).getPersonalPhotoList(type, sid, id, id_max, 
                    id_min, page_size);
        } catch (JSONException e) {
            
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long upload(Context context,UploadImageParams params) throws HttpException {
        
        try {
            return new NdFileStorageSdk(context).upload(params);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return 0;
    }

}
